package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BbgAdvice;
import com.ruoyi.system.service.IBbgAdviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员建议Controller
 * 
 * @author ruoyi
 * @date 2024-05-21
 */
@RestController
@RequestMapping("/system/advice")
public class BbgAdviceController extends BaseController
{
    @Autowired
    private IBbgAdviceService bbgAdviceService;

    /**
     * 查询会员建议列表
     */
    @PreAuthorize("@ss.hasPermi('system:advice:list')")
    @Log(title = "查询意见反馈列表", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(BbgAdvice bbgAdvice)
    {
        startPage();
        if (SecurityUtils.getDeptId()>100L && bbgAdvice.getDeptId() == null){
            bbgAdvice.setDeptId(SecurityUtils.getDeptId().intValue());
        }
        List<BbgAdvice> list = bbgAdviceService.selectBbgAdviceList(bbgAdvice);
        return getDataTable(list);
    }

    /**
     * 导出会员建议列表

    @PreAuthorize("@ss.hasPermi('system:advice:export')")
    @Log(title = "会员建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BbgAdvice bbgAdvice)
    {
        List<BbgAdvice> list = bbgAdviceService.selectBbgAdviceList(bbgAdvice);
        ExcelUtil<BbgAdvice> util = new ExcelUtil<BbgAdvice>(BbgAdvice.class);
        util.exportExcel(response, list, "会员建议数据");
    }
     */
    /**
     * 获取会员建议详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:advice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bbgAdviceService.selectBbgAdviceById(id));
    }

    /**
     * 新增会员建议

    @PreAuthorize("@ss.hasPermi('system:advice:add')")
    @Log(title = "会员建议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BbgAdvice bbgAdvice)
    {
        return toAjax(bbgAdviceService.insertBbgAdvice(bbgAdvice));
    }*/

    /**
     * 修改会员建议
     */
    @PreAuthorize("@ss.hasPermi('system:advice:edit')")
    @Log(title = "回复意见反馈", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BbgAdvice bbgAdvice)
    {
        if (bbgAdvice.getId() == null){
            return AjaxResult.error("缺失Id参数");
        }
        if (StringUtils.isEmpty(bbgAdvice.getAdviceType())){
            return AjaxResult.error("意见类型不能为空");
        }
        if (StringUtils.isEmpty(bbgAdvice.getResultContent())){
            return AjaxResult.error("处理结果不能为空");
        }

        BbgAdvice update = bbgAdviceService.selectBbgAdviceById(bbgAdvice.getId());
        if (update != null) {
            if (SecurityUtils.getDeptId()>100L && update.getDeptId() != SecurityUtils.getDeptId().intValue()){
                return AjaxResult.error("您没有权限处理该意见。");
            }
            //if (update.getStatus().equals("1")) {
                BbgAdvice data = new BbgAdvice();
                data.setId(update.getId());
                data.setAdviceType(bbgAdvice.getAdviceType());
                data.setResultContent(bbgAdvice.getResultContent());
                data.setStatus("2");
                data.setUpdateBy(SecurityUtils.getUsername());
                data.setUpdateTime(new Date());
                return toAjax(bbgAdviceService.updateBbgAdvice(data));
            //} else {
                //return AjaxResult.error("客户意见已经处理，请勿重复处理。");
            //}
        }
        return AjaxResult.error("没有找到相关信息。");
    }

    @PreAuthorize("@ss.hasPermi('system:advice:edit')")
    @Log(title = "设置是否可见", businessType = BusinessType.UPDATE)
    @PutMapping("/editShow")
    public AjaxResult editShow(@RequestBody BbgAdvice bbgAdvice)
    {
        if (bbgAdvice.getId() == null){
            return AjaxResult.error("缺失Id参数");
        }
        if (StringUtils.isEmpty(bbgAdvice.getIsShow())){
            return AjaxResult.error("是否可见不能为空");
        }

        BbgAdvice update = bbgAdviceService.selectBbgAdviceById(bbgAdvice.getId());
        if (update != null) {
            if (SecurityUtils.getDeptId()>100L && update.getDeptId() != SecurityUtils.getDeptId().intValue()){
                return AjaxResult.error("您没有权限处理该意见。");
            }
            BbgAdvice data = new BbgAdvice();
            data.setId(update.getId());
            data.setIsShow(bbgAdvice.getIsShow());
            data.setUpdateBy(SecurityUtils.getUsername());
            data.setUpdateTime(new Date());
            return toAjax(bbgAdviceService.updateBbgAdvice(data));
        }
        return AjaxResult.error("没有找到相关信息。");
    }

    /**
     * 删除会员建议

    @PreAuthorize("@ss.hasPermi('system:advice:remove')")
    @Log(title = "会员建议", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bbgAdviceService.deleteBbgAdviceByIds(ids));
    }
     */
}
